package com.yao.service.impl;

import com.yao.mapper.MoneyMapper;
import com.yao.pojo.ManageFinancial;
import com.yao.service.MoneyService;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 妖悲冥
 * @date 2021/5/17 0017   -    15:53
 */

@Service
public class MoneyServiceImpl implements MoneyService {
    @Autowired
    MoneyMapper moneyMapper;

    @Override
    public String addMoney(ManageFinancial financial) {
        financial.setFinancialDate();
        if (moneyMapper.addMoney(financial) > 0) {
            return "ok";
        }
        return "error";
    }

    @Override
    public List<ManageFinancial> selectMoneyOfId(ManageFinancial financial) {
        return moneyMapper.selectMoneyOfId(financial.getUserId());
    }

    /**
     * 获得一个星期内的预算
     *
     * @param userId 用户
     * @return 0：收入的 1：支出的
     */
    @Override
    public List<List<Integer>> selectAmountOfId(int userId) {
        //初始化7位，0-6对应星期一到星期天
        List<Integer> list;
        List<List<Integer>> listList = new ArrayList<>();
        Integer sum;
        int week;
        int financialId = 0;
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //6天前的星期
        cal.add(Calendar.DAY_OF_MONTH, -6);
        int currentWeek = cal.get(Calendar.DAY_OF_WEEK);
        System.out.println(currentWeek);
        //0对应星期一
        int weeks;
        String[] split = sdf.format(date).split("-");
        int today = Integer.parseInt(split[2]);
        //从6天前开始循环
        for (; financialId <= 1; financialId++) {
            week = 6;
            weeks = currentWeek;
            list = Lists.newArrayList(0, 0, 0, 0, 0, 0, 0);
            //循环星期
            for (; week >= 0; week--) {
                sum = moneyMapper.selectAmountOfId(userId, split[0] + "-" + split[1] + "-" + (today - week), financialId + 1);
                if (sum == null) {
                    sum = 0;
                }
                //0-6存星期7,1-6
                if (weeks == 1) {
                    list.set(6, sum);
                } else {
                    list.set(weeks - 2, sum);
                }
                weeks++;
                if (weeks > 7) {
                    weeks = 1;
                }
            }
            listList.add(list);
        }
        return listList;
    }

    @Override
    public List<Integer> selectPlayId(int userId) {
        List<Integer> list = new ArrayList<>();
        int playId = 4;
        for (int i = 1; i <= playId; i++) {
            list.add(moneyMapper.selectPlayId(userId, i));
        }
        return list;
    }

    @Override
    public List<Integer> selectMoneyId(int userId) {
        List<Integer> list = new ArrayList<>();
        int moneyId = 5;
        for (int i = 1; i <= moneyId; i++) {
            list.add(moneyMapper.selectMoneyId(userId, i));
        }
        return list;
    }

}
